package test_package;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 20.11.13
 * Time: 15:19
 * To change this template use File | Settings | File Templates.
 */
public class buxfer_com_menu_test_PO {

    public WebDriver driver;

    private static String site_url = "https://www.buxfer.com";
    private static String login = "destinygenerated@gmail.com";
    private static String password = "123123";
    private static String base_account_appendix_url = "/home#/home";

    private static String verification_data [][] = {                  //menu item, search keywords, xpath
            {"Profile", login,"/html/body/div[2]/div[2]/div[5]/div[2]/div[2]/div[5]/form/div/div[2]/table/tbody/tr/td"},
            {"Password", "Current password", "//div[contains(text(),'Current password')]"},
            {"Membership", "Membership plan","//div[contains(text(),'Membership plan')]"},
            {"Alerts", "Alert Type","/html/body/div[2]/div[2]/div[5]/div[6]/div[2]/div[5]/form/div/table/tbody/tr/th"},
            {"Currency", "Display format","/html/body/div[2]/div[2]/div[5]/div[7]/div[2]/div[5]/form/div[5]"},
            {"Help us translate", "Translate","/html/body/div[2]/div[2]/div[5]/div[8]/div/div/div[2]"},
            {"Automatic rules", "Add Tag Rule","/html/body/div[2]/div[2]/div[5]/div[9]/div[2]/div[5]/div/div[2]/table/tbody/tr[2]/td/div/a"},
            {"Download transaction", "Select the transactions you want to download:","/html/body/div[2]/div[2]/div[5]/div[10]/div[2]/div[3]/div/div"},
            {"Logout", "Login", "//a[contains(text(),'Login')]"}
        };



    @BeforeTest
    public void setUp(){
        WebDriver driver = new FirefoxDriver();
        driver.get(site_url);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.tagName("a"));
    }


    @Test
    public void buxfer_com_account_menu_test() throws IOException {
        buxfer_com_PO_MainPage main_page = new buxfer_com_PO_MainPage(driver);
        buxfer_com_PO_LoginPage login_page = main_page.clickLoginBtn();
        buxfer_com_PO_SignupPage signup_page = login_page.clickSignupLnk();
        buxfer_com_PO_AccountPage accountPage = signup_page.registerAccount(login, password);
        wait_for_page();
        accountPage.closePopUpWindow();
        wait_for_page();
        Assert.assertTrue(driver.getCurrentUrl().contentEquals(site_url + base_account_appendix_url));
        accountPage.clickOnMenu();
        accountPage.clickOnMenuItem("Profile");
        for (int i=0; i<(verification_data.length-1); i++){
        wait_for_page();
        if (accountPage.findText(verification_data[i][1],verification_data[i][2])) {
                System.out.println(verification_data[i][0]+" page verified!");
                accountPage.takeScreenshot(verification_data[i][0]);

            }
        }
        }

    @AfterTest
    public void teardown(){
        buxfer_com_PO_MainPage main_page = new buxfer_com_PO_MainPage(driver);
        buxfer_com_PO_AccountPage accountPage = main_page.login(login,password);
        wait_for_page();
        accountPage.delete_account(login, password);
        wait_for_page();
        Assert.assertFalse(driver.getCurrentUrl().contains("home"));
        wait_for_page();
        main_page.login(login, password);
        Assert.assertFalse(driver.getCurrentUrl().contains("home"));
        driver.quit();
    }
    public void wait_for_page () {
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.tagName("a"));

    }
}



